package classes;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;

public class part3 extends JFrame implements ActionListener {
	
	public static final int WINDOW_WIDTH = 400;
	public static final int WINDOW_HEIGHT = 400;
	public static final int CIRCLE_DIAMETER = 100;
	public static final int X_CIRCLE = 50;
	public static final int Y_CIRCLE = 50;
	public static final int NUMBER_OF_CHAR = 30;
	public static final int MAX_NUM_THREAD = 50;
	public static final int MAX_NUM_DART = 10000000;
	static int numThread;
	int numOfActiveThread;
	int numberOfDarts=0;
	final int pixels[][] = new int[MAX_NUM_DART][3];
	boolean flag = false;
	private static int dartInCircle = 0;
	
	
	private JTextField txtNumber;
	private JTextField txtNumberOfThread;
	private JLabel resultLabel;
	
	
	private class DrawingPanel extends JPanel
	 {
		public void paintComponent(Graphics g)
	 		{
			super.paintComponent(g);
			setBackground(Color.YELLOW);
			
			g.drawOval(X_CIRCLE, Y_CIRCLE, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
			g.drawRect(X_CIRCLE, X_CIRCLE, CIRCLE_DIAMETER, CIRCLE_DIAMETER);
	 		if(flag){
	 			 for (int idx = 0; idx < numberOfDarts; idx++){
	 				 if (pixels[idx][2] == 1){
	 				 g.setColor(Color.red);}
	 				 else {
	 					g.setColor(Color.blue);}
	 				 
	 				 g.drawLine(pixels[idx][0],pixels[idx][1],pixels[idx][0],pixels[idx][1]);
	 			 }
	 		}
	 		}
	 }
	public synchronized static void setDartInCircle (int dartThread){
		dartInCircle += dartThread; 
		
	}
	
public static void main(String[] args) {

		part3 window = new part3();
		window.setVisible(true);
	 }
	
	public part3()
	{
	 super("MonteCarlo");
	 setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 setLayout(new GridLayout(2, 1));
	 getContentPane().setBackground(Color.white);
	 
	 DrawingPanel circlePanel = new DrawingPanel();
	 JPanel dartPanel = new JPanel();
	 
	 circlePanel.setLayout(new BorderLayout());
	 dartPanel.setLayout(new FlowLayout());
	 dartPanel.setBackground(Color.GREEN);
	 txtNumberOfThread = new JTextField(NUMBER_OF_CHAR);
	 txtNumber = new JTextField(NUMBER_OF_CHAR);
	
	 JLabel dartLabel = new JLabel("Enter number of dart:");
	 JLabel NoOfThreadLabel = new JLabel("Enter number of threads:");
	 resultLabel = new JLabel();
	 //dartPanel.add(dartLabel, BorderLayout.LINE_END);
	
	 JButton actionButton = new JButton("Calculate Pi");
	 actionButton.addActionListener(this);
	 dartPanel.add(NoOfThreadLabel);
	 dartPanel.add(txtNumberOfThread, BorderLayout.NORTH);
	 dartPanel.add(dartLabel);
	 dartPanel.add(txtNumber, BorderLayout.NORTH);
	 dartPanel.add(actionButton);
	 dartPanel.add(resultLabel,BorderLayout.SOUTH);
	 add(circlePanel);
	 add(dartPanel);
	 
	
	}

	public void actionPerformed(ActionEvent e)
	{
		
		numThread = Integer.parseInt(txtNumberOfThread.getText());
		if (numThread <= 0 || numThread > MAX_NUM_THREAD){
			resultLabel.setForeground(Color.red);
			resultLabel.setText("Please enter a number between 1-"+MAX_NUM_THREAD);
			return;
			}

		numberOfDarts = Integer.parseInt(txtNumber.getText());
		if (numberOfDarts <= 0 || numberOfDarts > MAX_NUM_DART){
			resultLabel.setForeground(Color.red);
			resultLabel.setText("Please enter a number between 1-"+ MAX_NUM_DART);
			return;
			}
			
		numOfActiveThread = Thread.activeCount();
		
		
    	
		for (int i = 1;i <= numThread;i++){
    		new P3thread(numberOfDarts,numThread,pixels,i).start();
    	}
		
		float Pi = 0;
        flag = true;
        
        double startTime = System.currentTimeMillis();
        while(Thread.activeCount() > numOfActiveThread); 
        
        double duration = System.currentTimeMillis() - startTime ;
         Pi =  (float) 4*dartInCircle/numberOfDarts;
         resultLabel.setForeground(Color.black);
         resultLabel.setText("The Pi is: " +  Pi + " &The process time is:" 
        		 + duration + "ms");
         this.paintComponents(getGraphics());
         dartInCircle = 0;
	}

}


